<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="front/front_layout">
<head>
    <title>订单列表</title>
</head>
<body>
<section layout:fragment="content">
    <!--<div class="page-container">-->
        <div class="page-wrapper">
            <div class="table-wrapper">
                <div id="nav-waiter-content" class="content">
                    <div class="wrapper home-orders-body" >
                        <div class="table-order-list">
                            <div id="unconfirmed-orders" class="order-group" style="display: none;">
                                <div class="title"><div class="text">待处理</div><hr></div>
                            </div>

                            <div id="confirmed-orders" class="order-group" style="display: none;">
                                <div class="title"><div class="text">进行中</div><hr></div>
                            </div>

                            <div class="no-message" style="display: none">当前没有要处理的订单(^=^)</div>

                        </div>

                        <div id="repeat-waiter-home-order" class="order-g-item" style="display: none;">
                            <div class="order-des">
                                <div class="table-name">A12</div>
                                <div class="message">
                                    <div class="line waiter-home-order-number">订单编号：<span class="text">22</span></div>
                                    <div class="line waiter-home-order-time">下单时间：<span class="text">2016-09-18 10:14</span></div>
                                    <div class="line waiter-home-order-from">订单来源：<span class="text">微信(vision_小词)</span></div>
                                </div>
                            </div>
                            <div class="order-op">
                                <div class="price waiter-home-order-price">￥328</div>
                                <div class="btns">
                                    <button class="btn white waiter-home-order-append">加菜</button>
                                    <button class="btn waiter-home-order-pay">结账</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
            </div>
    <!--</div>-->
</section>

<section layout:fragment="extra_scripts">


    <script th:inline="javascript">

        (function () {
            "use strict";

            window.OrderState = {
                confirmed: 0, //已确认订单
                wechat_payed: 1, //已通过微信支付的订单
                unconfirmed: 2, // 为确认订单
                cart_message: 3, // 购物车信息
                scan_qr_payed: 4, // 扫描
                offline_payed: 5,
                refused: 6
            }


            window.initWaiter = function () {
                window.isWaiter = true;
                window.userId = $.cookie('userId') || 'codysnail';//'ohmygod';//'idiscody'; //测试使用
                window.storeId = $.cookie('storeId');
                window.username = $.cookie('username');

            }

            var isLogin = $.cookie('isLogin');
            if (!isLogin) {
                var $container = $('.page-container');
                $container.load('/snippet/login.html', '_t=23', function (text, status, $xhr) {

                });
            } else {
                initWaiter();
            }

            $('#waiter-name').find('.text').text($.cookie('username'));

            var $repeatWaiterHomeOrderItem = undefined;
            function cloneWaiterHomeOrderItem() {
                if(!$repeatWaiterHomeOrderItem){
                    $repeatWaiterHomeOrderItem = $('#repeat-waiter-home-order');
                }
                var $item = $repeatWaiterHomeOrderItem
                        .clone()
                        .show()
                        .attr('id','');
                return $item;
            }

            //订单
            function showStoreOrders() {

                $.getJSON('/waiterOrder/api/getStoreConfirmOrder/'+storeId+'/0','',function(data){
                    console.log(data);

                    if(data && data.total){
                        showOngoingOrders(data.result);
                    }

                });

                $.getJSON('/waiterOrder/api/getStoreNopayOrder/'+storeId+'/0','',function (data) {
                    console.log(data);

                    if(data && data.total){
                        showOngoingOrders(data.result);
                    }else{
                        showOngoingOrders();
                    }
                }).fail(function(){
                    showOngoingOrders();
                });
            }

            function resetWaiterHomeOrdersList() {
                $('#confirmed-orders').hide().children('.order-g-item').remove();
                $('#unconfirmed-orders').hide().children('.order-g-item').remove();
            }

            function setConfirmedOrderItem($item){
                if($('#confirmed-orders').is(':hidden')){
                    $('#confirmed-orders').show();
                }
                $item.find('.waiter-home-order-append').hide();
                $item.find('.waiter-home-order-pay').on('click',function(){

                    var tId = $(this).parents('.order-g-item').attr('table-id');
                    //收款
                    location.href="#action=waiter_qr&from=首页&tableId="+tId;

                    return false;
                }).text('结账');
            }

            function setUnconfirmedOrderItem($item){
                if($('#unconfirmed-orders').is(':hidden')){
                    $('#unconfirmed-orders').show();
                }

                $item.find('.waiter-home-order-append')
                        .show()
                        .on('click',function(){

                            return false;
                        }).text('拒绝');
                $item.find('.waiter-home-order-pay')
                        .show()
                        .on('click',function(){
                            return false;
                        }).text('确认');
            }
            function setOrderItemCommonStatus($item,orderItem){
                var orderId = orderItem.orderId;
                var time  = orderItem.createDate;
                var price = common.fixOne(orderItem.price);
                var tId = orderItem.tableId;
                var tableName = orderItem.tableName
                $item.attr('order-id',orderId);
                $item.attr('table-id',tId);
                $item.find('.table-name').text(tableName);
                $item.find('.waiter-home-order-number .text').text(orderId);
                $item.find('.waiter-home-order-time .text').text(time);
                $item.find('.waiter-home-order-price').text('￥'+ price);

                $item.on('click',function(event){
                    var oId = $(this).attr('order-id');
                    location.href="#action=waiter_order&orderId="+oId+'&from=首页';
                });
            }


            /**
             * 显示进行中的订单
             * @param data
             */
            function showOngoingOrders(orders) {
                if(!orders || !orders.length) { //没有订单，则显示提示消息
                    var height = $('#nav-waiter-content').height();
                    $('.home-orders-body .no-message').css({'line-height':height+'px'}).show();

                    return;
                }else{
                    $('.home-orders-body .no-message').hide();
                }

                resetWaiterHomeOrdersList();

                $.each(orders,function (index,orderItem) {
                    var $item = cloneWaiterHomeOrderItem();

                    setOrderItemCommonStatus($item,orderItem);
                    if(orderItem.state === OrderState.unconfirmed){

                        setUnconfirmedOrderItem($item);
                        $('#unconfirmed-orders').append($item);
                    }else if(orderItem.state === OrderState.confirmed){
                        setConfirmedOrderItem($item);
                        $('#confirmed-orders').append($item);
                    }
                });
            }
            showStoreOrders();

        }());
    </script>
</section>

</body>

</html>